<?php
/**
 * 
 * @authors 丛玉坤(you@example.org)
 * @date    2016-06-25 22:00:22
 * @version 一版
 */
namespace Home\Controller;
use Think\Controller;
class ActController extends Controller {
	public function _initialize(){
      	// 活动实例化
      	
      }
    
//活动首页
	public function index()
	{
			$Act=M('Huodong');
		// ->where('hstatus=1')
			$HD=$Act->join("in_hphotos on in_huodong.hid=in_hphotos.hid")->select(); 
			$this->HD=$HD;
		    $this->display();
	}
	//活动规则
	public function Myactivitys($ho)
	{
		$Act=M('Huodong');
		$HD=$Act->where("in_huodong.hid=".$ho)->join("in_hphotos on in_huodong.hid=in_hphotos.hid")->find(); 
		$this->HD=$HD;    
		$this->display();
	}
	//加入页
	public function addact()
	{
		$user=$_SESSION['id'];
		$ho=I("post.hid");
	   
		//判断用户登录
			if($user!=''){	
				//活动信息
                $Act=M('Huodong');
				$HD=$Act->where("in_huodong.hid=".$ho)->join("in_hphotos on in_huodong.hid=in_hphotos.hid")->find();

				//个人信息
				$US=M("User")->field("realname,userid,tel")->where('id='.$user)->find();   
							   
			    $use=time()+11*24*3600;
			    // $data['hid']=$HD['hid'];
			    $data['hname']=$HD['hname'];
			    $data['create_time']=date("Y-m-d H:i:s",time());
			    $data['jietime']=$HD['jietime'];
			    $data['usetime']=date("Y-m-d H:i:s",$use);
			    $data['username']=$US['realname'];
			    $data['userid']=$US['userid'];
			    $data['jstatus']=1;
			    $data['uid']=$user;
				 $wheres['hname']=$data['hname'];
				 $wheres['username']=$data['username'];
				 $wheres['userid']=$data['userid'];
				 // $wheres['jstatus']=1;
				 $wheres['hid']=$ho;

				    $Bbao=M("Hbaodan");

					$jiarujilu=$Bbao->where($wheres)->find();
					if (!empty($jiarujilu)) {
						echo 0;//加入过该计划
						die;
					}
					if ($US['realname']==''||$US['userid']=='') {
						echo 1;//未实名认证
						die;
					}
					if($US['tel']==''){
						echo 2;//手机未认证
						die;
					}

		   		$vip=M("Vip")->where('uid='.$user)->find();
		   	    $endtime=strtotime($vip['endtime']);
		   		$endvip=$endtime-time();
                if(!empty($vip)&&$endvip>0){
                	 // echo 6;//会员免费加入

						$B=$Bbao->add($data);
						if ($B) {
							echo 3;//加入活动
						}else{
							echo 4;//失败了！
						}
					
                }else{
              //不是会员加入
                	
					// 钱包信息
					$Qian=M("Qianbao");
					$QB=$Qian->field('money,zfpassword')->where('uid='.$user)->find();
					if ($QB['zfpassword']=='') {
						echo 5;//未设置支付密码
						die;
					}
				    $data['jstatus']=0;
				    $data['yufujin']=$HD['feiyong'];    
 					$bao['money']=$QB['money']-$HD['feiyong'];
					//扣钱
					$Q=$Qian->where("uid=".$user)->save($bao);
					// echo $Qian->getlastsql();
					if($Q){
					       $JL=M("Jilu");
							$j['uid']=$user;
				    		$j['jine']=$HD['feiyong'];
				    		$j['liuxiang']='9';
				    		$j['jiao_type']='0';
				    		$j['create_time']=date("Y:m:d H:i:s",time());
				    		$j['yue']=$bao['money'];

				    // 写入记录
				   		 $jilu=$JL->add($j);
				   		 // echo $JL->getlastsql();
 						    if ($jilu) {
						    //加入保单
							$B=$Bbao->add($data);	
							if ($B) {
								echo 6;//加入活动
							}else{
								echo 4;//失败了！
							}	    							
					    						
					      }						
					}

                }

	      	}else{
	      		echo "7";
		   	    

		   } 

	}
//会员须知
	public function articles()
	{
		$this->display();
	}
	public function actinfo()
	{
		$user=$_SESSION['id'];
		//判断用户登录
		// if ($_SESSION['id']!='') { 
			if($user!=''){	
				$HB=M("Hbaodan");
				$AB=$HB->join("in_hphotos on in_hbaodan.hbid=in_hphotos.hid")->select();
				$this->AB=$AB;
		   		$this->display();
	      	}else{
		   	    echo "<script>
		   	            alert('您未登录!请点击我登陆');
		   	            location.href='/Index/index';//客户端跳转
		   	          </script>";

		   } 

	}
	public function myacinfo()
	{
			 $b=I("get.bao");
			// die;
			if($b!='') {
			$Bao=M("Hbaodan");
			$baodan=$Bao->where('hbid='.$b)->join("in_hphotos on in_hbaodan.hbid=in_hphotos.hid")->join("in_huodong on in_hbaodan.hbid= in_huodong.hid")->find();   
			   
			$this->baodan=$baodan;

			//判断是否申请公示
			$tiaojian['gstatus']=1;
			$tiaojian['type']=1;
			$tiaojian['uid']=$_SESSION['id'];
			$tiaojian['hid']=$baodan['hbid'];
			$tiaojian['username']=$baodan['username'];
			$tiaojian['idcard']=$baodan['userid'];
			$tj=M("Gongshi")->where($tiaojian)->find();
			//判断等待期
			  $end=strtotime($baodan['usetime'])-time();
			  if($end>0){
				$this->dengdai=2;//未到等待期
			  }else{
			  	$this->dengdai=1;//过等待期
			  }	   
				$this->tiao=$tj; 
			} 
			 $this->display();
		}

		public function addwelfare()
		{
		$Uarr=M("User")->where('id='.$_SESSION['id'])->find();	
		$this->type=$type=I("get.type");
		$gid=I("get.gid")?$gid=I("get.gid"):$gid=I("post.gid");
		$this->gid=$gid;
		$Binfo=M("Hbaodan")->where('hbid='.$gid)->find();
		if (IS_POST) {
				 $upload = new \Think\Upload();// 实例化上传类
				 $upload->saveName = time().'_'.mt_rand();
				 $upload->maxSize   =     3145728 ;// 设置附件上传大小
				 $upload->exts      =     array("docx");// 设置附件上传类型
				 $upload->savePath  =      '/'; // 设置附件上传目录// 上传文件 
				 $info   =   $upload->upload();if(!$info) {// 上传错误提示错误信息    
				 $this->error($upload->getError());
				}else{
				// 上传成功 获取上传文件信息    
				foreach($info as $file){        
					 $data['Wsrc']= 'Uploads'.$file['savepath'].$file['savename'];    
				}} 
				$data['sname']=$Binfo['hname'];
				$data['bid']=$Binfo['bid'];
				// $data['fcardid']=$Uarr['userid'];
				// $data['fuser']=$Uarr['realname'];
				$data['buser']=$Uarr['realname'];
				$data['hd']=$Binfo['hd'];
				$data['bcardid']=$Binfo['userid'];
				$data['uid']=$_SESSION['id'];
				$data['miaoshu']=I("post.content");
				$data['createtime']=date("Y-m-d H:i:s");

				$wheres['sname']=$Binfo['hname'];
				$wheres['bid']=$Binfo['bid'];
				$wheres['uid']=$_SESSION['id'];
				// $wheres['fcardid']=$data['fcardid'];
				// $wheres['fuser']=$Uarr['realname'];
				$wheres['hd']=$Binfo['hd'];
				$wheres['buser']=$data['buser'];
				$wheres['bcardid']=$Binfo['userid'];
				$Shen=M("Huoshen");
				$unique=$Shen->where($wheres)->find();
				if ($unique!='') {
					    
					    echo "<script>
					            alert('申请过了不要重复申请');
					            history.go(-3);
					          </script>";
				}else{

					$shen=$Shen->add($data);
					if ($shen) {
						    
						    echo "<script>
						            alert('申请成功！请耐心等待');
						            history.go(-2);
						          </script>";
					}
				}			    

		}else{

   
		$this->display();
		}
	}


	
}